styleproperty: Convert background-image from pattern to GtkCssImage
authorBenjamin Otte <otte@redhat.com>
Mon, 2 Jan 2012 21:22:25 +0000 (22:22 +0100)
committerBenjamin Otte <otte@redhat.com>
Mon, 9 Jan 2012 17:37:56 +0000 (18:37 +0100)
gtk/gtkcssstyleproperty.c
gtk/gtkcssstylepropertyimpl.c

index 47918572434cd56d5abb7b2dcf8feaf616282b48..da1a6b708cf6e2168bd1745f49121ebffeb950cf 100644 (file)
@@ -28,6 +28,9 @@
 #include "gtkprivatetypebuiltins.h"
 #include "gtkstylepropertiesprivate.h"
 
+#include "gtkcssimagegradientprivate.h"
+#include "gtkcssimageprivate.h"
+
 enum {
   PROP_0,
   PROP_ID,
@@ -131,7 +134,36 @@ _gtk_css_style_property_query (GtkStyleProperty   *property,
   
   val = _gtk_style_properties_peek_property (props, GTK_CSS_STYLE_PROPERTY (property), state);
   if (val)
-    g_value_copy (val, value);
+    {
+      /* Somebody make this a vfunc */
+      if (G_VALUE_TYPE (val) == GTK_TYPE_CSS_IMAGE)
+        {
+          GtkCssImage *image = g_value_get_object (val);
+          cairo_pattern_t *pattern;
+          cairo_surface_t *surface;
+          cairo_matrix_t matrix;
+          
+          if (image == NULL)
+            g_value_set_boxed (value, NULL);
+          else if (GTK_IS_CSS_IMAGE_GRADIENT (image))
+            g_value_set_boxed (value, GTK_CSS_IMAGE_GRADIENT (image)->pattern);
+          else
+            {
+              double width, height;
+
+              /* the 100, 100 is rather random */
+              _gtk_css_image_get_concrete_size (image, 0, 0, 100, 100, &width, &height);
+              surface = _gtk_css_image_get_surface (image, NULL, width, height);
+              pattern = cairo_pattern_create_for_surface (surface);
+              cairo_matrix_init_scale (&matrix, width, height);
+              cairo_pattern_set_matrix (pattern, &matrix);
+              cairo_surface_destroy (surface);
+              g_value_take_boxed (value, pattern);
+            }
+        }
+      else
+        g_value_copy (val, value);
+    }
   else
     _gtk_style_property_default_value (property, props, state, value);
 }
index 57e01a44ed2977b684118759c4d7c6f95c4d2a37..b3f4965bf66e73da283661ea0f4a0631fa4c3f39 100644 (file)
@@ -36,6 +36,7 @@
 /* the actual parsers we have */
 #include "gtkanimationdescription.h"
 #include "gtkbindings.h"
+#include "gtkcssimageprivate.h"
 #include "gtkgradient.h"
 #include "gtkshadowprivate.h"
 #include "gtkthemingengine.h"
@@ -339,6 +340,57 @@ border_corner_radius_value_print (GtkCssStyleProperty *property,
     }
 }
 
+static gboolean 
+css_image_value_parse (GtkCssStyleProperty *property,
+                       GValue              *value,
+                       GtkCssParser        *parser,
+                       GFile               *base)
+{
+  GtkCssImage *image;
+
+  if (_gtk_css_parser_try (parser, "none", TRUE))
+    image = NULL;
+  else
+    {
+      image = _gtk_css_image_new_parse (parser, base);
+      if (image == NULL)
+        return FALSE;
+    }
+
+  g_value_unset (value);
+  g_value_init (value, GTK_TYPE_CSS_IMAGE);
+  g_value_take_object (value, image);
+  return TRUE;
+}
+
+static void
+css_image_value_print (GtkCssStyleProperty *property,
+                       const GValue        *value,
+                       GString             *string)
+{
+  GtkCssImage *image = g_value_get_object (value);
+
+  if (image)
+    _gtk_css_image_print (image, string);
+  else
+    g_string_append (string, "none");
+}
+
+static void
+css_image_value_compute (GtkCssStyleProperty    *property,
+                         GValue                 *computed,
+                         GtkStyleContext        *context,
+                         const GValue           *specified)
+{
+  GtkCssImage *image = g_value_get_object (specified);
+
+  if (image)
+    image = _gtk_css_image_compute (image, context);
+
+  g_value_init (computed, GTK_TYPE_CSS_IMAGE);
+  g_value_take_object (computed, image);
+}
+
 /*** REGISTRATION ***/
 
 #define rgba_init(rgba, r, g, b, a) G_STMT_START{ \
@@ -608,13 +660,15 @@ _gtk_css_style_property_init_properties (void)
                                           &value);
   g_value_unset (&value);
 
-  gtk_style_property_register            ("background-image",
+  g_value_init (&value, GTK_TYPE_CSS_IMAGE);
+  _gtk_style_property_register           ("background-image",
                                           CAIRO_GOBJECT_TYPE_PATTERN,
                                           0,
-                                          NULL,
-                                          NULL,
-                                          NULL,
-                                          NULL);
+                                          css_image_value_parse,
+                                          css_image_value_print,
+                                          css_image_value_compute,
+                                          &value);
+  g_value_unset (&value);
   gtk_style_property_register            ("background-repeat",
                                           GTK_TYPE_CSS_BACKGROUND_REPEAT,
                                           0,